#! /bin/sh

#---------------------------------------------------------------------
#                              bootstrap                              
# 
# This will be called by the template bootstrap function, which is
# itself called by fw-bootstrap, which is in turn invoked by the
# bootstrap executable deposited at the top of the source project.
# It is intended for running any setup activity required prior to
# building.
#---------------------------------------------------------------------

#---------------------------------------------------------------------
#                           chicken and egg                           
#---------------------------------------------------------------------

test -f "fw/sh/fw-find.sh" && fws="fw/sh/fw-find.sh"
test -f "fw.local/sh/fw-find.sh" && fws="fw.local/sh/fw-find.sh"
test ! -z "$fws" || {
  echo "C/bootstrap: fatal: can't locate fw-find.sh" 1>&2
  exit 1
}

. "$fws"

#---------------------------------------------------------------------
#                              routines                               
#---------------------------------------------------------------------

fw_source "C/bootstrap" "sh/bootstrap-util.sh"
fw_source "C/bootstrap" "sh/fatal.sh"
fw_source "C/bootstrap" "sh/fw-exec.sh"
fw_source "C/bootstrap" "sh/parse-args.sh"
fw_source "C/bootstrap" "sh/validate-args.sh"

check_alternatives () \
  {
    local _check_rv
    local _check_save
    local _check_var

    _check_save="$1"

    if test "x$_check_save" = x
      then
        echo "C/bootstrap: error: bad call to check_alternatives" 1>&2
        fatal "C/bootstrap"
      fi

    shift

    _check_var="$1"
    _check_rv=""

    while test "x$_check_var" != x 
      do
        _check_rv=`which "$_check_var" 2> /dev/null`
        if test "x$_check_rv" != x 
          then
            break
          fi
        shift
        _check_var="$1"
      done

    if test "x$_check_rv" = x
      then
        echo "C/bootstrap: error: can't find alternative for $_check_save" 1>&2
        fatal "C/bootstrap"
      fi

    eval $_check_save='"$_check_rv"'
  }

#---------------------------------------------------------------------
#                                main                                 
#---------------------------------------------------------------------

parse_args "C/bootstrap" "$@"

validate_args "C/bootstrap" "revision" "revision"

for dir in . src tests
  do
    for x in Makefile_dot_am            \
             configure.ac               \
             pkgconfig-template.pc.in   \
             test-wrapper.sh.in
      do
        target=`echo "$x" | perl -pe 's/_dot_am/.am/;'`

        bootstrap_link_optional "template/C/$dir/$x"            \
                                "$dir"                          \
                                "$target"                       \
        || fatal "C/bootstrap"
      done
  done

check_alternatives mylibtoolize glibtoolize libtoolize libtoolize15
check_alternatives myautoheader autoheader259 autoheader257 autoheader
check_alternatives pkgconfig pkg-config

"$mylibtoolize" --automake || fatal "C/bootstrap"

fw-exec "revision/$revision/ignore-files" config.sub depcomp ltmain.sh \
|| fatal "C/bootstrap"

fw-exec "template/C/make-pkgconfig-template"                 \
        `find src -name '*.am' -or -name '*.am.local'`       \
  > pkgconfig-template.pc.in                                 \
|| fatal "C/bootstrap"

fw_exec "build/automake/bootstrap" "$@" --autoheader "$myautoheader"
